package com.cq.oa.service.sm.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.service.impl.BaseServiceImpl;
import oa.base.session.SessionManger;
import oa.base.util.Constants;

import com.cq.oa.dao.sm.RoleResDao;
import com.cq.oa.dao.sm.UserRoleDao;
import com.cq.oa.domain.sm.Role;
import com.cq.oa.domain.sm.RoleRes;
import com.cq.oa.domain.sm.UserRole;
import com.cq.oa.service.sm.RoleService;

public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{

	
	private UserRoleDao userRoleDao;
	
	private RoleResDao roleResDao;
	
	
	
	public void setRoleResDao(RoleResDao roleResDao) {
		this.roleResDao = roleResDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#createRole(com.cq.oa.domain.sm.Role)
	 */
	public Long createRole(Role role) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#updateRole(com.cq.oa.domain.sm.Role)
	 */
	public void updateRole(Role role) {
		
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#deleteRole(com.cq.oa.domain.sm.Role)
	 */
	public void deleteRole(Role role) {
		
	}

	public void doRoleUnUser(Long roleId,String roleName,List<Long> userIds,String[] userNames) {
		List<UserRole> roles = new ArrayList<UserRole>();
		for(int i=0;i<userIds.size();i++){
			UserRole role = new UserRole();
			role.setUserSid(userIds.get(i));
			role.setUserCname(userNames[i]);
			role.setRoleSid(roleId);
			role.setRoleCname(roleName);
			roleResDao.initBase(role);
			roles.add(role);
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(Constants.ORG_SID, SessionManger.getOrgId());
		params.put("roleSid", roleId);
		this.userRoleDao.remove(params);
		this.userRoleDao.saveAll(roles);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#doRoleUnRes(java.lang.Long, java.util.List)
	 */
	public void doRoleUnRes(Long roleId,String roleName,List<Long> resIds,String[] resNames) {
		List<RoleRes> roles = new ArrayList<RoleRes>();
		for(int i=0;i<resIds.size();i++){
			RoleRes role = new RoleRes();
			role.setResSid(resIds.get(i));
			role.setResCname(resNames[i]);
			role.setRoleSid(roleId);
			role.setRoleCname(roleName);
			roleResDao.initBase(role);
			roles.add(role);
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(Constants.ORG_SID, SessionManger.getOrgId());
		params.put("roleSid", roleId);
		this.roleResDao.remove(params);
		this.roleResDao.saveAll(roles);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#findRoleResById(java.lang.Long)
	 */
	public List<RoleRes> findRoleResById(Long roleId) {
		if(roleId==null){
			return new ArrayList<RoleRes>();
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(Constants.ORG_SID, SessionManger.getOrgId());
		params.put("roleSid", roleId);
		return this.roleResDao.find(params);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.sm.RoleService#findUserRoleById(java.lang.Long)
	 */
	public List<UserRole> findUserRoleById(Long roleId) {
		if(roleId==null){
			return new ArrayList<UserRole>();
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(Constants.ORG_SID, SessionManger.getOrgId());
		params.put("roleSid", roleId);
		return this.userRoleDao.find(params);
	}

	
}
